package com.wangjw.concurrency.controller;

import com.wangjw.concurrency.object.*;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wangjw on 18/5/28.
 */
@Controller
public class MvcController {

    @RequestMapping(value = "baseType.do")
    @ResponseBody
    public String baseType(@RequestParam("xage") int age) {
        return "age: " + age;
    }

    @RequestMapping(value = "baseType2.do")
    @ResponseBody
    public String baseType2(Integer age) {
        return "age: " + age;
    }

    // http://localhost:8081/array.do?name=wangjw&name=nijj&name=wangxm
    @RequestMapping(value = "array.do")
    @ResponseBody
    public String array(String[] name) {
        StringBuilder builder = new StringBuilder();
        for (String item : name) {
            builder.append(item).append(" ");
        }
        return builder.toString();
    }

    // http://localhost:8081/object.do?name=wangjw&age=28
    // http://localhost:8081/object.do?name=wangjw&age=28&contactInfo.phone=10086&contactInfo.address=hhht
    @RequestMapping(value = "object.do")
    @ResponseBody
    public String object(User user) {
        return user.toString();
    }

    // http://localhost:8081/objects.do?user.name=wangjw&user.age=28&contactInfo.phone=10086&admin.name=nijj&admin.age=25
    @RequestMapping(value = "objects.do")
    @ResponseBody
    public String objects(User user, Admin admin) {
        return user.toString() + "  " + admin.toString();
    }

    @InitBinder("user")
    public void initUser(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");
    }

    @InitBinder("admin")
    public void initAdmin(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("admin.");
    }

    // http://localhost:8081/list.do?users[0].name=wangjw&users[1].name=nijj
    // http://localhost:8081/list.do?users[0].name=wangjw&users[1].name=nijj&users[20].name=wangmz
    @RequestMapping(value = "list.do")
    @ResponseBody
    public String list(UserListForm userListForm) {
        return "list size: " + userListForm.getUsers().size() + "  " + userListForm.toString();
    }

    // http://localhost:8081/set.do?users[0].name=wangjw&users[1].name=wangjw
    @RequestMapping(value = "set.do")
    @ResponseBody
    public String set(UserSetForm userSetForm) {
        return "set size: " + userSetForm.getUsers().size() + "  " + userSetForm.toString();
    }

    // http://localhost:8081/map.do?users[%22X%22].name=wangjw&users[%22X%22].age=31&users[%22Y%22].name=nijj&users[%22Y%22].age=30
    @RequestMapping(value = "map.do")
    @ResponseBody
    public String map(UserMapForm userMapForm) {
        return "map size: " + userMapForm.getUsers().size() + "  " + userMapForm.toString();
    }

    /*{
        "name": "wangjw",
        "age": 28,
        "contactInfo": {
            "phone": "10086",
            "address": "hhht"
        }
    }*/
    // application/json
    @RequestMapping(value = "json.do")
    @ResponseBody
    public String json(@RequestBody User user) {
        return user.toString();
    }

    /*<?xml version="1.0" encoding="UTF-8"?>
    <admin>
        <name>wangjw</name>
        <age>31</age>
    </admin>*/
    // application/xml
    @RequestMapping(value = "xml.do")
    @ResponseBody
    public String xml(@RequestBody Admin admin) {
        return admin.toString();
    }

    // http://localhost:8081/date1.do?date1=2018-05-29
    @RequestMapping(value = "date1.do")
    @ResponseBody
    public String date1(Date date1) {
        return date1.toString();
    }

    @InitBinder("date1") // 只限于当前Controller
    public void initDate1(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @RequestMapping(value = "book", method = RequestMethod.GET)
    @ResponseBody
    public String book(HttpServletRequest request) {
        String contentType = request.getContentType();
        if (contentType == null) {
            return "book.default";
        } else if (contentType.equals("txt")) {
            return "book.txt";
        } else if (contentType.equals("html")) {
            return "book.html";
        }
        return "book.default";
    }

    // http://localhost:8081/subject/10086
    @RequestMapping(value = "/subject/{subjectId}", method = RequestMethod.GET)
    @ResponseBody
    public String subjectGet(@PathVariable("subjectId") String subjectId) {
        return "this is a get method, subjectId: " + subjectId;
    }

    @RequestMapping(value = "/subject/{subjectId}", method = RequestMethod.POST)
    @ResponseBody
    public String subjectPost(@PathVariable("subjectId") String subjectId) {
        return "this is a post method, subjectId: " + subjectId;
    }

    @RequestMapping(value = "/subject/{subjectId}", method = RequestMethod.DELETE)
    @ResponseBody
    public String subjectDelete(@PathVariable("subjectId") String subjectId) {
        return "this is a delete method, subjectId: " + subjectId;
    }

    @RequestMapping(value = "/subject/{subjectId}", method = RequestMethod.PUT)
    @ResponseBody
    public String subjectPut(@PathVariable("subjectId") String subjectId) {
        return "this is a put method, subjectId: " + subjectId;
    }
}
